package com.itfreer.power.service.power.strategy;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.itfreer.form.dictionary.DictionaryManage;
import com.itfreer.form.dictionary.base.DictionaryItem;

/**
 * 图层权限数据提供接口
 */
@Component("MemoryMapLayersJurisdictionDataProvide")
public class MemoryMapLayersJurisdictionDataProvide implements MapLayersJurisdictionDataProvide {

	@Autowired
	private DictionaryManage dictionaryManage;

	private static final String mapLayersDic = "p_maplayer_jurisdiction";

	@SuppressWarnings("unchecked")
	@Override
	public Map<String, List<String>> getMapLayers(List<String> mapLayerIDs, String serviceId) {
		if (mapLayerIDs == null || mapLayerIDs.size() <= 0) {
			return null;
		}
		Map<String, List<String>> itemCache = new HashMap<String, List<String>>();
		for (String mapLayer : mapLayerIDs) {
			DictionaryItem item = dictionaryManage.getDictionary(mapLayersDic, mapLayer);
			if (item != null && item.getOther().get("registerService").toString().equals(serviceId)) {
				if (item.getOther().containsKey("items") && item.getOther().get("items") != null) {
					Map<String, List<String>> items = (Map<String, List<String>>) item.getOther().get("items");
					for (String key : items.keySet()) {
						if (itemCache.containsKey(key)) {
							for (String tw : items.get(key)) {
								if (!itemCache.get(key).contains(tw)) {
									itemCache.get(key).add(tw);
								}
							}
						} else {
							itemCache.put(key, items.get(key));
						}
					}
				}
			}
		}
		return itemCache;
	}

}
